package org.nlp2rdf.navigator.benchmark;

import com.google.common.collect.Sets;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
import org.aksw.commons.semweb.sparql.core.*;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.Options;
import virtuoso.jena.driver.VirtGraph;

import java.io.File;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * Created by Claus Stadler
 * Date: Oct 20, 2010
 * Time: 7:54:08 PM
 */
public class CategoryWikilinkCorrelation {

    private static Options cliOptions = new Options()
        .addOption("c", "categoryFile", true, "File containing a list of category names");


    public static void main(String[] args)
            throws Exception
    {
        CommandLineParser cliParser = new GnuParser();
        CommandLine commandLine = cliParser.parse(cliOptions, args);

        List<Resource> categories = BenchmarkUtils.readResources(commandLine.getOptionValue("c"));
/*
        if(commandLine.hasOption("c")) {
            String categoryFileName = commandLine.getOptionValue("c");

            if(categoryFileName.equals("") || categoryFileName.equals("-")) {
                categories = HistorySimulationBenchmark.readResources(null);
            } else {
                categories = HistorySimulationBenchmark.readResources(new File(categoryFileName));
            }
        } else {
            categories = HistorySimulationBenchmark.readResources(null);
        }
*/

        Collection<String> graphNames = Collections.singleton("http://dbpedia.org");
        Property wikiLink = ResourceFactory.createProperty("http://dbpedia.org/property/wikilink");
        ISparqlEndpoint coreSparqlEndpoint = new HttpSparqlEndpoint("http://hanne.aksw.org:8895/sparql");

        //coreSparqlEndpoint = new DelayingSparqlEndpoint(coreSparqlEndpoint, 1000);
        //ISparqlEndpoint coreSparqlEndpoint = new VirtuosoSparqlEndpoint(virtGraph, "http://dbpedia.org");

        ISparqlEndpoint sparqlEndpoint = new CachingSparqlEndpoint(coreSparqlEndpoint, "/tmp/sparqlCache");

        for(Resource categoryClass : categories) {
            Resource wikiLinkClass = ResourceFactory.createResource(categoryClass.toString().replace("Category:", ""));


            Set<Resource> categoryResources = MySparqlTasks.getSubjectsByPredicateObject(sparqlEndpoint, graphNames, Skos.subject, categoryClass);
            Set<Resource> wikiLinkResources = MySparqlTasks.getSubjectsByPredicateObject(sparqlEndpoint, graphNames, wikiLink, wikiLinkClass);

            Set<Resource> common = Sets.intersection(categoryResources, wikiLinkResources);


            //Set<Resource> catWoWl = Sets.difference(categoryResources, wikiLinkResources);
            //Set<Resource> wlWoCat = Sets.difference(wikiLinkResources, categoryResources);

            //double coverage = common.size() / (double)categoryResources.size();

            System.out.println(categoryClass + "\t" + common.size() + "\t" + categoryResources.size() + "\t" + wikiLinkResources.size() + "\t");
            //System.out.println(categoryClass + "\t" + common.size() + "\t" + categoryResources.size() + "\t" + wikiLinkResources.size() + "\t");
        }

    }
    /**
     * Maybe a better approach than this one: (ok, doesn't seem like we need it)
     * Select ?s ?cat ?wl From <http://dbpedia.org> {
     *     ?s skos:subject ?cat .
     *     Filter(?cat Like <http://dbpedia.org/resource/Category:%>) .
     *     Optional {
     *         ?s wikiLink
     *     }
     * }
     *
     *
     * @param args
     * @throws Exception
     */
    public static void main2(String[] args)
            throws Exception
    {


        //VirtGraph virtGraph = new VirtGraph("http://dbpedia.org", "http://hanne.aksw.org:1112", "dba", "dba");
        Collection<String> graphNames = Collections.singleton("http://dbpedia.org");

        Property wikiLink = ResourceFactory.createProperty("http://dbpedia.org/property/wikilink");

        ISparqlEndpoint coreSparqlEndpoint = new HttpSparqlEndpoint("http://hanne.aksw.org:8892/sparql");

        //coreSparqlEndpoint = new DelayingSparqlEndpoint(coreSparqlEndpoint, 1000);

        //ISparqlEndpoint coreSparqlEndpoint = new VirtuosoSparqlEndpoint(virtGraph, "http://dbpedia.org");
        ISparqlEndpoint sparqlEndpoint = new CachingSparqlEndpoint(coreSparqlEndpoint, "/tmp/sparqlCache");
        //sparqlEndpoint = new DelayingSparqlEndpoint(sparqlEndpoint, 20);

        Iterable<QuerySolution> qc = new QueryCollection(sparqlEndpoint,
                "Select Distinct ?cat From <http://dbpedia.org> {?s skos:subject ?cat . Filter(?cat Like <http://dbpedia.org/resource/Category:%>) . } Order By Asc(?cat)").asJava();

        for(QuerySolution qs : qc) {
            Resource categoryClass = qs.getResource("?cat");
            Resource wikiLinkClass = ResourceFactory.createResource(categoryClass.toString().replace("Category:", ""));


            Set<Resource> categoryResources = MySparqlTasks.getSubjectsByPredicateObject(sparqlEndpoint, graphNames, Skos.subject, categoryClass);
            Set<Resource> wikiLinkResources = MySparqlTasks.getSubjectsByPredicateObject(sparqlEndpoint, graphNames, wikiLink, wikiLinkClass);

            Set<Resource> common = Sets.intersection(categoryResources, wikiLinkResources);


            //Set<Resource> catWoWl = Sets.difference(categoryResources, wikiLinkResources);
            //Set<Resource> wlWoCat = Sets.difference(wikiLinkResources, categoryResources);

            //double coverage = common.size() / (double)categoryResources.size();

            System.out.println(categoryClass + "\t" + common.size() + "\t" + categoryResources.size() + "\t" + wikiLinkResources.size() + "\t");
            //System.out.println(categoryClass + "\t" + common.size() + "\t" + categoryResources.size() + "\t" + wikiLinkResources.size() + "\t");
        }
    }
}
